#include "iotgo_platformAPI.h"
#include "iotgo_heart_beat.h"
#include "iotgo_communicate_with_long_server.h"

#if (PLATFORM == PLATFORM_RTL8711 || PLATFORM == PLATFORM_ESP8266)
static xTaskHandle heart_beat_task;
static MySocketClient *p_socket = NULL;
static volatile uint8_t state = 0;
static uint8_t heartBeat_interval = 0;

static void heart_beat_thread(void *parameter)
{
    int8_t heart_beat_data[6]= {0x89,0x80,0x00,0x00,0x00,0x00};
    while(1)
    {
	systemDelayMs(10000);
        /*send websocket Ping to Server*/
	printf("[%s] send heart beat\n",__FUNCTION__);
        writeToServer(p_socket,heart_beat_data, 6);
    }
}

int runHeartBeat(MySocketClient *p_mysocket, uint8_t second)
{
    if(state == 0)
    {
	p_socket = p_mysocket;
	heartBeat_interval = second;
	if(xTaskCreate(heart_beat_thread, 
		       ((const char*)"heart_beat_thread"), 
		       200, 
		       NULL,
		       tskIDLE_PRIORITY + 1,
		       &heart_beat_task) != pdPASS)
	{	
	    printf("heart beat task\n");
	    return -1;
	}
	state = 1;
    }
    return 0;
}

int stopHeartBeat(void)
{
    if(state == 1)
    {
        vTaskDelete(heart_beat_task);
        state = 0;
    }
    return 0;
}

int runHeartBeatWithA6(MySocketClient *p_mysocket, uint8_t second)
{
    p_socket = p_mysocket;
    heartBeat_interval = second;
    return 0;
}

int stopHeartBeatWithA6(void)
{
    if(state == 1)
    {
        vTaskDelete(heart_beat_task);
        state = 0;
    }
    return 0;
}


#elif (PLATFORM == PLATFORM_LINUX)
#include <stdio.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
pthread_t heart_beat_task;
static MySocketClient *p_socket = NULL;
static volatile uint8_t state = 0;

void my_os_delay(uint32_t msec)
{
    int tfd,epfd,ret;
    struct epoll_event ev;
    struct itimerspec ts;

    tfd = timerfd_create(CLOCK_REALTIME,0);
    if(tfd == -1)
    {
	printf("timerfd_create() failed\n");
    }
    ts.it_interval.tv_sec = 0;
    ts.it_interval.tv_nsec = 0;
    ts.it_value.tv_sec = msec / 1000;
    ts.it_value.tv_nsec = (msec % 1000) * 1000000;

    if(timerfd_settime(tfd,0,&ts,NULL) < 0)
    {
	printf("timerfd_settime() failed\n");
	close(tfd);
    }

    epfd = epoll_create(1);
    if(epfd == -1)
    {
	printf("epoll_create() failed\n");
	close(tfd);
    }

    ev.events = EPOLLIN;

    if(epoll_ctl(epfd, EPOLL_CTL_ADD,tfd,&ev) == -1)
    {
	printf("epoll_ctl() failed\n");
	close(epfd);
	close(tfd);
    }

    ret = epoll_wait(epfd,&ev,1,msec + 1000);
    if(ret < 0)
    {
	printf("epoll_wait failed\n");
	close(epfd);
	close(tfd);
    }

    close(epfd);
    close(tfd);
}


static void *heart_beat_thread(void *parameter)
{
    int8_t heart_beat_data[6]= {0x89,0x80,0x00,0x00,0x00,0x00};
    int8_t count = 0;

    while(1)
    {
	count++;
        /*send websocket Ping to Server*/
	printf("[%s] !!!heart_beat [%d]\n",__FUNCTION__,count);
        writeToServer(p_socket,heart_beat_data, 6);
	//systemDelayMs(3000);
	my_os_delay(5000);
    }
}

int runHeartBeat(MySocketClient *p_mysocket, uint8_t second)
{
    pthread_create(&heart_beat_task,NULL,heart_beat_thread,NULL);
    p_socket = p_mysocket;
    state = 1;
    return 0;
}

int stopHeartBeat(void)
{
    if(state == 1)
    {
	pthread_cancel(heart_beat_task);
        state = 0;
    }
    return 0;
}

#else
#endif


